<%@page import="Servlet.dataBaseController"%>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.io.File" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="com.mysql.jdbc.Connection"%>
<%@ page import="java.sql.*"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <center><h3>The Stock has been Uploaded</h2><br/>
     <h4>Redirecting in 5 seconds</h3>

 </center>
   <%!
     String symbol="";         
         String sname="";
         String type="";
     float price=0;
         int count1=0,count2=0,count3=0,count4=0;
         String itemName="";
 %>
 <%
 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
 if (!isMultipart) {
 } else {
   FileItemFactory factory = new DiskFileItemFactory();
   ServletFileUpload upload = new ServletFileUpload(factory);
   List items = null;
   try {
   items = upload.parseRequest(request);
   } catch (FileUploadException e) {
   session.setAttribute("error", "Please validate your input");
   response.sendRedirect("/CSE305/add_stock_invalid.jsp");
   return;
   }
   Iterator itr = items.iterator();
   while (itr.hasNext()) 
           {
   FileItem item = (FileItem) itr.next();
   if (item.isFormField())
           {
              String name = item.getFieldName();
                  String value = item.getString();
                  if(name.equals("symbol"))
                   {
                           symbol=value;
                         count1=1;
                           }
                          if(name.equals("name"))
                          {  
                                         sname=value;                         
                         count2=2;
                                          }
                          if(name.equals("type"))
                          {  
                                         type=value;                         
                            count4=4;
                                          }
                          if(name.equals("price"))
                          {
                               try{
                                 price=Float.parseFloat(value);
                                 count3=3;}
                               catch(Exception e)
                 {session.setAttribute("error", "Please validate your input");
                response.sendRedirect("/CSE305/add_stock_invalid.jsp");
                               return;}
                                          }
                                                          
                                 
                       } else
           {
    try {
           itemName = item.getName();
   //File savedFile = new File("/Users/lgj1626/NetBeansProjects/trunk/web/images/"+itemName);
           if(itemName != null)
           {File savedFile = new File(config.getServletContext().getRealPath("/")+"images/"+itemName);
   
   item.write(savedFile);}
     %>
     <center></table><table ><tr><td></td><td><img  border="2" src="../images/<%=itemName %>" width="137"  height="140" /></td></tr></table>
     <table
 border="2" width="350">
      <% if(count1==1)
                 out.println("<tr><td align='left'><b>Stock Symbol:</td><td><b>"+symbol);
          if(count2==2)  
                     out.println("</td><tr><td align='left'><b>Name:</td><td><b>"+sname);
          if(count4==4)  
                     out.println("</td><tr><td align='left'><b>Type</b></td><td><b>"+type);
          if(count3==3)
                     out.println("</td><tr><td align='left'><b>Price</td><td><b>"+price);

   } catch (Exception e) {
   e.printStackTrace();
   }
   }
   }
   }
 
    Connection con = null;
    Statement st = null;
 
    try {
                con = (Connection) dataBaseController.getConnection();
                st = (Statement) con.createStatement();

                st.executeUpdate("INSERT INTO `brchung`.`stock` (`StockSymbol`, `CompanyName`, `Type`, `PricePerShare`, `img`) VALUES "
                        + "('"+ symbol +"', '"+sname+"', '"+type+"', "+ price +", '"+ itemName +"');");
                st.executeUpdate("INSERT INTO `brchung`.`stockhistory` (`stockid`, `Date`, `PricePerShare`) VALUES ("
                        + "'"+ symbol +"', NOW() , "+ price +");");
            } catch (SQLException s) {
                session.setAttribute("error", "Duplicate StockSymbol");
                response.sendRedirect("/CSE305/add_stock_invalid.jsp");
                return;

            }
 
   %>
     </td></tr></table></center>
     <%
        response.setHeader("Refresh", "5;URL=/CSE305/index.jsp");
     %>